package com.nexgo.payment.action.online.upload;

import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.constrant.OfflineState;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.TransRecord;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.TransUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaox on 2017/9/5.
 */

/**
 * 批上送磁条卡联机交易
 */
class BatchUploadMagOnline extends AbsBatchUpload {
    private final Logger log;
    private final List<TransRecord> mTransRecords;
    private List<TransRecord> mMagOnlineTransRecords;
    private int mIdx = 0;

    BatchUploadMagOnline(DataSource repository) {
        super(repository);
        log = LoggerFactory.getLogger(getClass().getSimpleName());
        mTransRecords = mRepository.getSuccessfulTransRecords();
    }

    @Override
    TransType getTransType() {
        return TransType.BATCH_UPLOAD_MAG_ONLINE;
    }

    @Override
    String getTip() {
        return App.sContext.getString(R.string.batch_upload);
    }

    @Override
    TransData buildTransData() {
        TransData transData = new TransData();
        transData.setTransType(TransType.BATCH_UPLOAD_MAG_ONLINE);
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(SysConstant.LOCALE, "%02d", mMagOnlineTransRecords.size()));
        for (TransRecord transRecord : mMagOnlineTransRecords) {
            sb.append(transRecord.getIsInternational() ? "01" : "00");
            sb.append(StringUtils.leftPad(transRecord.getTraceNum(), 6, '0'));
            sb.append(StringUtils.leftPad(transData.getPan(), 20, '0'));
            sb.append(StringUtils.leftPad(transData.getAmount(), 12, '0'));
        }
        transData.setTemporary(sb.toString());
        return transData;
    }

    @Override
    TransRecord hasNext() {
        mMagOnlineTransRecords = new ArrayList<>();
        for (int i = mIdx; i < mTransRecords.size(); i++) {
            TransRecord transRecord = mTransRecords.get(i);
            //批上送成功的跳过
            if (transRecord.getIsBatchUpload()) {
                continue;
            }
            //不是联机的跳过
            if (!OfflineState.ONLINE.equals(OfflineState.values()[transRecord.getOfflineState()])) {
                continue;
            }

            //lr add扫码交易跳过
            TransType transType = TransType.values()[transRecord.getTransType()];
            if(TransUtils.isScanTrans(transType)){
                continue;
            }
            //是IC卡跳过
            if (TransUtils.isICCTransByEntryMode(transRecord.getEntryMode())) {
                continue;
            }
            mMagOnlineTransRecords.add(transRecord);
            mIdx = i + 1;
            if (mMagOnlineTransRecords.size() == 8) {
                return new TransRecord();
            }
        }
        return mMagOnlineTransRecords.size() > 0 ? new TransRecord() : null;
    }

    @Override
    void updateBatchUploadState() {
        for (TransRecord transRecord : mMagOnlineTransRecords) {
            log.debug("更新批上送成功状态，流水号{}", transRecord.getTraceNum());
            transRecord.setIsBatchUpload(true);
            mRepository.updateTransRecord(transRecord);
        }
    }
}
